import cv2 
import numpy as np

#读取图片
image = cv2.imread('Car-Front-21x21.png')

#获取图片尺寸
height, width = image.shape[:2]
print(height, width)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, bin = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY_INV)  # 黑→白


y_idx, x_idx = np.where(bin == 255)#找白色像素点
print('白点个数:', x_idx.size)
print('重心列:', x_idx.mean())

if x_idx.size == 0:          # 丢线保护
    dx = 0
else:
    cx = x_idx.mean()        # 0~19
    
    dx = cx - 10           # 窗口中心列
    
e = dx / 10   # -1 ~ 1
print('e:', e)
delta_theta_deg = e * 180.0   # 一度一格，满量程 ±180°
print(delta_theta_deg*np.pi/180)
print(np.cos(delta_theta_deg*np.pi/180))

k= 5#先用比例来计算
delta = k * e